home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHAP14.TXT < prev    next >
Text File  |  1989-12-30  |  15KB  |  323 lines

  1.                      Chapter 14 - Example Programs
  2.  
  3.  
  4.                              WHY THIS CHAPTER?
  5.  
  6.              Although  every  program  in this tutorial has  been  a 
  7.         complete  program,  each  one  has also been  a  very  small 
  8.         program intended to teach you some principle of  programming 
  9.         in  C.   It  would do you a disservice to leave you at  that 
  10.         point  without introducing you to a few larger  programs  to 
  11.         illustrate  how  to  put together the  constructs  you  have 
  12.         learned  to create a major program.   This chapter  contains 
  13.         four  programs  of increasing complexity,  each designed  to 
  14.         take  you  into a higher plateau of  programming,  and  each 
  15.         designed to be useful to you in some way.
  16.  
  17.              DOSEX will illustrate how to make DOS system calls  and 
  18.         will teach you,  through self-study, how the system responds 
  19.         to  the  keyboard.   WHATNEXT  reads commands input  on  the 
  20.         command line and will aid you in setting up a variable batch 
  21.         file,  one  that requests an operator input and responds  to 
  22.         the  input  by branching to a different part  of  the  batch 
  23.         file.
  24.  
  25.              LIST  is  the source code for the program you  used  to 
  26.         print  out the C source files when you began studying C with 
  27.         the aid of this tutorial.  Finally we come to VC, the Visual 
  28.         Calculator,  which  you should find to be a  useful  program 
  29.         even  if you don't study its source code.   VC uses most  of 
  30.         the  programming  techniques we have studied in this  course 
  31.         and  a few that we never even mentioned such  as  separately 
  32.         compiled subroutines.
  33.  
  34.              We  will  take a look at the example programs one at  a 
  35.         time  but  without  a complete explanation of  any  of  them 
  36.         because  you  have  been studying C for some  time  now  and 
  37.         should be able to read and understand most of these programs 
  38.         on  your  own.   One other thing must  be  mentioned,  these 
  39.         programs  use  lots of nonstandard constructs and  you  will 
  40.         probably need to modify some of them to get them to  compile 
  41.         with  your  particular  compiler.  That will be left  as  an 
  42.         exercise for you.
  43.  
  44.                      DOSEX.C - The DOS Example Program
  45.  
  46.              The  copy of DOS that you received with your IBM-PC  or 
  47.         compatible has about 50 internal DOS calls that you can  use 
  48.         as  a programmer to control your peripheral devices and read 
  49.         information  or status from them.   Some of the earlier  IBM 
  50.         DOS manuals, DOS 2.0 and earlier, have these calls listed in 
  51.         the back of the manual along with how to use them.   Most of 
  52.         the  manuals  supplied  with compatible  computers  make  no 
  53.         mention  of  these  calls even  though  they  are  extremely 
  54.         useful.   These  calls  can  be  accessed  from  nearly  any 
  55.  
  56.  
  57.                                   Page 94
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                      Chapter 14 - Example Programs
  68.  
  69.  
  70.         programming  language but they do require some initial study 
  71.         to learn how to use them.   This program is intended to  aid 
  72.         you in this study.
  73.  
  74.              Display the program on your monitor or print it out for 
  75.         reference.   It  is  merely a loop watching for  a  keyboard 
  76.         input or a change in the time.  If either happens, it reacts 
  77.         accordingly.   In line 23,  the function "kbhit()" returns a 
  78.         value  of 1 if a key has been hit but not yet read from  the 
  79.         input buffer by the program.  This is a nonstandard function 
  80.         and  may require a name change for your particular compiler.  
  81.         There will probably be several similar calls that will  need 
  82.         changed  for  your compiler in order to compile and run  the 
  83.         programs in chapter 14.
  84.  
  85.              Look at the function named "get_time" for an example of 
  86.         a  DOS call.   An interrupt 21(hex) is called after  setting 
  87.         the  AH  register to 2C(hex) =  44(decimal).   The  time  is 
  88.         returned in the CH,  CL, and DH registers.  Refer to the DOS 
  89.         call  definitions in your copy of DOS.   If the  definitions 
  90.         are  not included there,  Peter Nortons  book,  "Programmers 
  91.         Guide  to  the  IBM PC" is recommended as a  good  reference 
  92.         manual   for   these  calls  and  many   other   programming 
  93.         techniques.
  94.  
  95.              Another useful function is the "pos_cursor()"  function 
  96.         that  positions the cursor anywhere on the monitor that  you 
  97.         desire  by  using  a  DOS  interrupt.   In  this  case,  the 
  98.         interrupt  used  is  10(hex) which is  the  general  monitor 
  99.         interrupt.   This particular service is number 2 of about 10 
  100.         different  monitor  services  available.    This  particular 
  101.         function  may  not be needed by your compiler  because  some 
  102.         compilers  have a cursor positioning function predefined for 
  103.         your use.  This function is included here as another example 
  104.         to you.
  105.  
  106.              The  next  function,  service  number  6  of  interrupt 
  107.         10(hex)  is the window scroll service.   It should  be  self 
  108.         explanatory.
  109.  
  110.              In this program, the cursor is positioned and some data 
  111.         is  output  to the monitor,  then the cursor is "hidden"  by 
  112.         moving  it  to line 26 which is not  displayed.   After  you 
  113.         compile and run the program, you will notice that the cursor 
  114.         is  not  visible on the monitor.   This is possible  in  any 
  115.         program,  but  be  sure  to put the cursor  in  view  before 
  116.         returning  to  DOS  because  DOS does not  like  to  have  a 
  117.         "hidden" cursor and may do some strange things. 
  118.  
  119.              Some time spent studying this program will be  valuable 
  120.         to  you as it will reveal how the keyboard data is input  to 
  121.  
  122.  
  123.                                   Page 95
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                      Chapter 14 - Example Programs
  134.  
  135.  
  136.         the  computer.   Especially of importance is how the special 
  137.         keys such as function keys, arrows, etc. are handled.
  138.  
  139.                   WHATNEXT.C - The Batch File Interrogator
  140.  
  141.              This  is  an  example of how to read the  data  on  the 
  142.         command line following the function call.  Notice that there 
  143.         are  two variables listed within the  parentheses  following 
  144.         the main() call.   The first variable is a count of words in 
  145.         the entire command line including the command itself and the 
  146.         second  variable  is  a  pointer to  an  array  of  pointers 
  147.         defining the actual words on the command line.
  148.  
  149.              First the question on the command line, made up of some 
  150.         number of words, is displayed on the monitor and the program 
  151.         waits for the operator to hit a key.   If the key hit is one 
  152.         of  those  in the last "word" of the group of words  on  the 
  153.         command  line,  the number of the character within the group 
  154.         is  returned to the program where it can be tested with  the 
  155.         "errorlevel" command in the batch file.   You could use this 
  156.         technique  to  create a variable AUTOEXEC.BAT  file  or  any 
  157.         other  batch  file  can  use this for  a  many  way  branch.  
  158.         Compile  and  run this file with TEST.BAT for an example  of 
  159.         how  it  works in practice.   You may  find  this  technique 
  160.         useful  in  one  of  your batch files and  you  will  almost 
  161.         certainly  need  to  read in  the  command  line  parameters 
  162.         someday.
  163.  
  164.              An  interesting alternative would be for you to write a 
  165.         program  named "WOULD.C" that would return a 1 if a  "Y"  or 
  166.         "y"  were typed and a zero if any other key were hit.   Then 
  167.         your batch file could have a line such as;
  168.  
  169.         WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
  170.  
  171.              Dos would use "WOULD" as the program name,  ignore  the 
  172.         rest  of  the  statement  except for displaying  it  on  the 
  173.         screen.   You  would  then respond to the  question  on  the 
  174.         monitor  with a single keyhit.   Your batch file would  then 
  175.         respond   to  the  1  or  0  returned  and  either  run  the 
  176.         alternative  part  of  the batch file or  the  primary  part 
  177.         whatever each part was.
  178.  
  179.         WOULD YOU LIKE PRIMARY (Y/N)
  180.         IF ERRORLEVEL 1 GOTO PRIMARY
  181.         (secondary commands)
  182.         GOTO DONE
  183.         :PRIMARY
  184.         (primary commands)
  185.         :DONE
  186.  
  187.  
  188.  
  189.                                   Page 96
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                      Chapter 14 - Example Programs
  200.  
  201.  
  202.                         LIST.C - The Program Lister
  203.  
  204.              This program is actually composed of two files,  LIST.C 
  205.         and  LISTF.C  that must be separately  compiled  and  linked 
  206.         together  with your linker.   There is nothing new here  and 
  207.         you  should  have  no  trouble compiling  and  linking  this 
  208.         program  by  reading the documentation  supplied  with  your 
  209.         compiler.
  210.  
  211.              The  only  thing  that is new in this  program  is  the 
  212.         inclusion   of  three  "extern"  variables  in  the  LISTF.C 
  213.         listing.   The only purpose for this is to tie these  global 
  214.         variables  to  the main program and tell the  compiler  that 
  215.         these  are not new variables.   The compiler will  therefore 
  216.         not  generate any new storage space for them but simply  use 
  217.         their names during the compile process.   At link time,  the 
  218.         linker  will  get  their actual storage locations  from  the 
  219.         LIST.OBJ  file and use those locations for the variables  in 
  220.         the  LISTF part of the memory map also.   The  variables  of 
  221.         those  names in both files are therefore the same  identical 
  222.         variables and can be used just as any other global variables 
  223.         could be used if both parts of the program were in one file.
  224.  
  225.              There is no reason why the variables couldn't have been 
  226.         defined  in the LISTF.C part of the program and declared  as 
  227.         "extern"  in the LIST.C part.   Some of the variables  could 
  228.         have  been  defined  in one and some in the  other.   It  is 
  229.         merely a matter of personal taste.   Carried to an  extreme, 
  230.         all of the variables could have been defined in a third file 
  231.         and  named "extern" in both of these files.   The third file 
  232.         would then be compiled and included in the linking process.
  233.  
  234.              It would be to your advantage to compile, link, and run 
  235.         this  program to prepare you for the next program  which  is 
  236.         composed of 5 separate files which must all work together.
  237.  
  238.                         VC.C - The Visual Calculator
  239.  
  240.              This  program  finally ties nearly everything  together 
  241.         because  it uses nearly every concept covered in the  entire 
  242.         tutorial.   It  is so big that I will not even try to  cover 
  243.         the finer points of its operation.   Only a few of the  more 
  244.         important points will be discussed.
  245.  
  246.              The  first  thing  you  should do  is  go  through  the 
  247.         tutorial  for  VC included in the file  VC.DOC.   There  are 
  248.         several  dozen  steps  for you to execute,  with  each  step 
  249.         illustrating some aspect of the Visual Calculator.  You will 
  250.         get  a  good feel for what it is capable of doing  and  make 
  251.         your study of the source code very profitable.  In addition, 
  252.         you  will  probably  find  many  ways  to  use  the   Visual 
  253.  
  254.  
  255.                                   Page 97
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                      Chapter 14 - Example Programs
  266.  
  267.  
  268.         Calculator  to  solve problems involving calculations  where 
  269.         the  simplicity  of  the problem at hand  does  not  warrant 
  270.         writing a program.
  271.  
  272.              Notice that the structure definitions,  used in all  of 
  273.         the  separate parts of the program,  are defined in the file 
  274.         STRUCT.DEF.   During  program development,  when  it  became 
  275.         necessary  to change one of the structures slightly,  it was 
  276.         not  necessary to change it in all of the  files,  only  one 
  277.         file  required modification which was then "included" in the 
  278.         source files.   Notice that the transcript data is stored in 
  279.         a doubly linked list with the data itself being stored in  a 
  280.         separate  dynamically allocated char string.   This line  is 
  281.         pointed to by the pointer "lineloc".
  282.  
  283.              For  ease  of development,  the similar functions  were 
  284.         grouped together and compiled separately.   Thus, all of the 
  285.         functions  involving the monitor were included in  the  file 
  286.         named  VIDEO.C,  and all of the functions involving the data 
  287.         storage were grouped into the FILE.C  collection.   Dividing 
  288.         your  program  in  a  way similar to  this  should  simplify 
  289.         debugging and future modifications. 
  290.  
  291.              Of special interest is the "monitor()" function.   This 
  292.         function  examines  the  video mode through  use  of  a  DOS 
  293.         command  and  if it is a 7,  it assumes it is  a  monochrome 
  294.         monitor,  otherwise it assumes a color monitor.   The colors 
  295.         of  the various fields are established at this time and used 
  296.         throughout  the  program.   Most  of  the  data  is  written 
  297.         directly  to the video memory,  but some is written  through 
  298.         the standard BIOS routines.
  299.  
  300.              The file DEFIN.C is simply a catalogue of the functions 
  301.         to aid in finding the functions.  This file was generated as 
  302.         one  of the first files and was maintained and  updated  for 
  303.         use during the entire design and coding lifetime.
  304.  
  305.              Feel free,  after understanding this code, to modify it 
  306.         in any way you desire for your own use.
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                   Page 98
  322.  
  323.